<template>
  <div class="search-page">
    <div class="search-header">
      <el-input
        v-model="searchQuery"
        placeholder="搜索新闻关键词..."
        clearable
        @keyup.enter="performSearch"
      >
        <template #append>
          <el-button icon="Search" @click="performSearch" />
        </template>
      </el-input>
    </div>
    
    <div v-if="state.isLoading" class="loading-container">
      <el-progress type="circle" :percentage="70" :show-text="false"></el-progress>
      <p>正在搜索新闻...</p>
    </div>
    
    <div v-else>
      <h2 class="section-title">搜索结果: "{{ searchQuery }}"</h2>
      
      <div v-if="categoryNews.length" class="news-grid">
        <NewsCard 
          v-for="item in categoryNews" 
          :key="item.id"
          :news="item"
        />
      </div>
      
      <div v-else class="no-results">
        <el-empty description="没有找到相关新闻" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { storeToRefs } from 'pinia';
import { useNewsStore } from '@/stores/news';
import NewsCard from '@/components/NewsCard.vue';

const newsStore = useNewsStore();
const { state, categoryNews } = storeToRefs(newsStore);
const { search } = newsStore;

const searchQuery = ref('');

const performSearch = async () => {
  if (searchQuery.value.trim()) {
    await search(searchQuery.value.trim());
  }
};
</script>

<style scoped>
.search-page {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.search-header {
  margin-bottom: 30px;
  max-width: 600px;
  margin: 0 auto 30px;
}

.section-title {
  margin: 20px 0;
  font-size: 24px;
  color: #333;
  font-weight: 600;
}

.news-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

.loading-container {
  text-align: center;
  padding: 60px 0;
}

.loading-container p {
  margin-top: 20px;
  font-size: 18px;
  color: #409EFF;
}

.no-results {
  padding: 40px 0;
  text-align: center;
}

@media (max-width: 768px) {
  .news-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 16px;
  }
  
  .section-title {
    font-size: 20px;
  }
}
</style>